package com.quanyan.assets.web;


import com.alibaba.rocketmq.common.message.MessageExt;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.AssetsPayReceive;
import com.quanyan.api.enums.AssetsType;
import com.quanyan.api.enums.Currency;
import com.quanyan.assets.controller.Test;
import com.quanyan.assets.emun.AssetsErrorEnum;
import com.quanyan.assets.emun.OpType;
import com.quanyan.assets.emun.StatusType;
import com.quanyan.assets.entity.*;
import com.quanyan.assets.mapper.*;
import com.quanyan.assets.request.*;
import com.quanyan.assets.response.*;
import com.quanyan.assets.service.*;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.exception.BizException;
import com.quanyan.common.utils.MD5Utils;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.mq.client.QcMsgClient;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.orderpay.request.ReqOrderRefund;
import com.quanyan.orderpay.response.mq.OrderNotifyMsg;
import com.quanyan.orderpay.response.mq.RefundNotifyMsg;
import com.quanyan.orderpay.service.OrderPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wmb on 2016/5/27.
 */
@RestController
@RequestMapping(value="/test")
public class TestController {
    @Autowired
    private TbAssetsTypeMapper tbAssetsTypeMapper;
    @Autowired
    private AssetsRuleService tbAssetsRuleService;
    @Autowired
    private UserAssetsService userAssetsService;
    @Autowired
    private CurrencyService currencyService;
    @Autowired
    private TestService testService;
    @Autowired
    private ETbUserAssetsMapper eTbUserAssetsMapper;
    @Autowired
    private TbUserAssetsMapper tbUserAssetsMapper;
    @Autowired
    private AssetsRuleService assetsRuleService;
    @Autowired
    private MqConsumerService mqConsumerService;
    @Autowired
    private TbAccumulateRuleMapper tbAccumulateRuleMapper;
    @Autowired
    private TbUserEggsAccumulateHistoryMapper tbUserEggsAccumulateHistoryMapper;
    @Autowired
    private UserAccumulateHistoryService userAccumulateHistoryService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private UtilCouponService utilCouponService;
    @Autowired
    private TbCouponInfoMapper tbCouponInfoMapper;
    @Autowired
    private UtilAccumulateService utilAccumulateService;
    private Logger logger = LoggerFactory.getLogger(TestController.class);
    @RequestMapping(value="/test",method= RequestMethod.POST)
    public ResponseEntity<Object> test(HttpServletRequest request){
        APIRequest apiRequest=new APIRequest();
        Byte id= Currency.getIdByName("EGGS");


//        APIRequest apiRequest=new APIRequest();
//
//
//        TbAssetsTypeExample example=new TbAssetsTypeExample();
//        int count=tbAssetsTypeMapper.countByExample(example);
//        return new ResponseEntity<Object>(count, HttpStatus.OK);
//        TbAssetsRule tbAssetsRule=new TbAssetsRule();
//        tbAssetsRule.setSourceSystemId(1);
//        apiRequest.setData(tbAssetsRule);
//        tbAssetsRuleService.add(apiRequest);
//        return new ResponseEntity<Object>(count, HttpStatus.OK);
//        APIRequest apiRequest=new APIRequest();
//        ReqUserAccumulateModify reqUserAccumulateModify=new ReqUserAccumulateModify();
//        apiRequest.setData(reqUserAccumulateModify);
//        userAssetsService.modifyAccumulate(apiRequest);
//        APIRequest apiRequest=new APIRequest();
  //      apiRequest.setData(new Test());
//        Integer age=testService.getAge(apiRequest);
//        System.out.println(currencyService.exchangeRatioByName("EGGS"));
//        System.out.println(currencyService.exchangeRatioById(Currency.getIdByName("EGGS")));
//        APIRequest apiRequest=new APIRequest();
//        ReqRuleDetail reqRuleDetail=new ReqRuleDetail();
//        reqRuleDetail.setSourceSystemId(1);
//        reqRuleDetail.setSourceBusinessId(1);
//        reqRuleDetail.setUid(1);
//        reqRuleDetail.setOrderAmount(new BigDecimal(100));
//        apiRequest.setData(reqRuleDetail);
//        APIResponse<List<RespRuleDetail>> apiResponse=userAssetsService.getUserAccumulateAndRule(apiRequest);
//        System.out.print("1");
//        ReqUserAccumulateModify reqUserAccumulateModify=new ReqUserAccumulateModify();
//        reqUserAccumulateModify.setSourceSystemId(1);
//        reqUserAccumulateModify.setSourceBusinessId(1);
//        reqUserAccumulateModify.setUid(1);
//        reqUserAccumulateModify.setBusinessId(1);
//        reqUserAccumulateModify.setAssetsType((byte)1);
//        reqUserAccumulateModify.setOrderAmount(new BigDecimal(30));
//        apiRequest.setData(reqUserAccumulateModify);
//        APIResponse<RespUserAccumulateModify> apiResponse=userAssetsService.modifyAccumulate(apiRequest);


//        ReqUserAccumulateModify reqUserAccumulateModify = new ReqUserAccumulateModify();
//        reqUserAccumulateModify.setSystemCode("C");
//        reqUserAccumulateModify.setBusinessCode("B");
//        reqUserAccumulateModify.setUid(11);
//        reqUserAccumulateModify.setAssetsType((byte) 1);
//        apiRequest.setData(reqUserAccumulateModify);
//        APIResponse apiResponse=userAssetsService.modifyAccumulate(apiRequest);
//        System.out.print(apiResponse);
//        StopWatch stopwatch=new StopWatch();
//        stopwatch.start();
//        eTbUserAssetsMapper.initInsert(14, (byte) 1);
//        stopwatch.stop();
//        System.out.println("initInsert<<<<<<<<<<<<<<<<"+stopwatch.getTime());
//        stopwatch.reset();
//        stopwatch.start();
//        TbUserAssetsExample tbUserAssetsExample=new TbUserAssetsExample();
//        tbUserAssetsExample.createCriteria().andUidEqualTo(15).andAssetsTypeEqualTo((byte)1);
//        int count=tbUserAssetsMapper.countByExample(tbUserAssetsExample);
//        if(count==0){
//            TbUserAssets tbUserAssets=new TbUserAssets();
//            tbUserAssets.setUid(15);
//            tbUserAssets.setAccumulate(0L);
//            tbUserAssets.setUseableAmount(0);
//            tbUserAssets.setAssetsType((byte)1);
//            tbUserAssets.setState((byte)1);
//            tbUserAssetsMapper.insertSelective(tbUserAssets);
//        }
////        stopwatch.stop();
////        System.out.println("151<<<<<<<<<<<<<<<<"+stopwatch.getTime());
////        stopwatch.reset();
////        stopwatch.start();
////        TbUserAssets tbUserAssets1=new TbUserAssets();
////        tbUserAssets1.setUid(15);
////        tbUserAssets1.setAccumulate(0L);
////        tbUserAssets1.setUseableAmount(0);
////        tbUserAssets1.setAssetsType((byte)1);
////        tbUserAssets1.setState((byte)1);
////        tbUserAssetsMapper.insertSelective(tbUserAssets1);
//        stopwatch.stop();
//        System.out.println("152<<<<<<<<<<<<<<<<"+stopwatch.getTime());
//        stopwatch.reset();
//        stopwatch.start();
//        TbUserAssets tbUserAssets2=new TbUserAssets();
//        tbUserAssets2.setUid(16);
//        tbUserAssets2.setAccumulate(0L);
//        tbUserAssets2.setUseableAmount(0);
//        tbUserAssets2.setAssetsType((byte)1);
//        tbUserAssets2.setState((byte)1);
//        tbUserAssetsMapper.insertSelective(tbUserAssets2);
//        System.out.println("16<<<<<<<<<<<<<<<<" + stopwatch.getTime());
//        ReqRuleDetail reqRuleDetail=new ReqRuleDetail();
//        reqRuleDetail.setSystemCode("C");
//        reqRuleDetail.setBusinessCode("C");
//        reqRuleDetail.setUid(10);
//        reqRuleDetail.setOrderAmount(new BigDecimal(100));
//        apiRequest.setData(reqRuleDetail);

//        ReqRefund reqRefund=new ReqRefund();
//        reqRefund.setSystemCode("CLUB_SYSTEM_CODE");
//        reqRefund.setRefundBusinessCode("CLUB_ACTIVITY_REFUND_ORDER_BUSINESS_CODE");
//        reqRefund.setBusinessCode("CLUB_ACTIVITY_ORDER_BUSINESS_CODE");
//        reqRefund.setBusinessNo("123456");
//        reqRefund.setReceiver("18681444372");
//        reqRefund.setInfo("退款测试");
//        List<AccumulateAssets> accumulateAssetsList=new ArrayList<AccumulateAssets>();
//        AccumulateAssets accumulateAssets=new AccumulateAssets();
//        accumulateAssets.setAssetsType(AssetsType.ASSETS_EGGS.id.byteValue());
//        accumulateAssets.setAccumulate(100L);
//        accumulateAssetsList.add(accumulateAssets);
//        reqRefund.setAccumulateAssetsList(accumulateAssetsList);
//        apiRequest.setData(reqRefund);
//        APIResponse apiResponse=userAssetsService.refund(apiRequest);
//        ReqUserAssets reqUserAssets=new ReqUserAssets();
//        reqUserAssets.setUid(10001);
//        apiRequest.setData(reqUserAssets);
//        APIResponse apiResponse= userAssetsService.add(apiRequest);
//
//        OrderNotifyMsg orderNotifyMsg = new OrderNotifyMsg();
//        orderNotifyMsg.setUid(462);
//
//        orderNotifyMsg.setOrderAmount(new BigDecimal(0.1));
//
//        orderNotifyMsg.setOutTradeNo("F21874337214500046285");
//        orderNotifyMsg.setPayAmount(new BigDecimal(30));
//        orderNotifyMsg.setResultCode(OrderNotifyMsg.RESULT_CODE_USER_PAY_SUCCESS);
//        orderNotifyMsg.setSourceSystemCode(Constants.ASSETS_SYSTEM_CODE);
//        orderNotifyMsg.setSourceBusinessCode(Constants.ASSETS_EGGS_RECHANGE_CODE);
//
//        MessageExt messageExt=new MessageExt();
//        messageExt.setBody(ObjectUtil.toByteArray(orderNotifyMsg));
//        mqConsumerService.eggsRechange(messageExt);



//        testService.test1();
//        TbAccumulateRuleExample tbAccumulateRuleExample=new TbAccumulateRuleExample();
//        TbAccumulateRuleExample.Criteria criteria=tbAccumulateRuleExample.createCriteria();
//        criteria.andSystemCodeEqualTo("PLACE_SYSTEM_CODE");
//        criteria.andBusinessCodeEqualTo("PLACE_BOOKING_BUSINESS_CODE");
//
//            criteria.andAssetsTypeEqualTo((byte)1);
//
//        List<TbAccumulateRule> tbAccumulateRuleList=tbAccumulateRuleMapper.selectByExample(tbAccumulateRuleExample);
//        if(tbAccumulateRuleList.isEmpty()){
//            throw new BizException(AssetsErrorEnum.ASSETS_RULE_RECORD_DO_NOT_EXISTS);
//        }
//        System.out.println(tbAccumulateRuleList.size());
        //testService.test1();
//        ReqUserAccumulateHistory reqUserAccumulateHistory=new ReqUserAccumulateHistory();
//        reqUserAccumulateHistory.setAssetsType(AssetsType.ASSETS_SHELL.id.byteValue());
//        List<Byte> statusList=new ArrayList<Byte>();
//        statusList.add((byte)1);
//        reqUserAccumulateHistory.setUid(3697);
//        reqUserAccumulateHistory.setStartCreateTime("2016-08-03 00:00:00");
//        reqUserAccumulateHistory.setEndCreateTime("2016-09-03 00:00:00");
//
//        reqUserAccumulateHistory.setStatusList(statusList);
//        apiRequest.setData(reqUserAccumulateHistory);
//        String md5Sign = MD5Utils.MD5Encode("" + Constants.SECRET_KEY);
//        String md5Sign1 = MD5Utils.MD5Encode(null + Constants.SECRET_KEY);
        logger.info("退款资产系统调用时间开始");
        ReqRefund reqRefund = new ReqRefund();;
        ReqAccumulateRefund reqAccumulateRefund = new ReqAccumulateRefund();
        reqAccumulateRefund.setSystemCode("PLACE_SYSTEM_CODE");
        reqAccumulateRefund.setBusinessCode("PLACE_BOOKING_BUSINESS_CODE");
        reqAccumulateRefund.setBusinessNo("A2016071803");
        List<AccumulateAssets> accumulateAssetsList = new ArrayList<AccumulateAssets>();
        AccumulateAssets accumulateAssets = new AccumulateAssets();
        accumulateAssets.setAccumulate((long)1);
        accumulateAssets.setAssetsType(AssetsType.ASSETS_EGGS.id.byteValue());
        accumulateAssetsList.add(accumulateAssets);
        reqAccumulateRefund.setAccumulateAssetsList(accumulateAssetsList);
        reqAccumulateRefund.setInfo("测试");
        reqRefund.setReqAccumulateRefund(reqAccumulateRefund);
        APIRequest<ReqRefund> refundAssets = new APIRequest<ReqRefund>();
        refundAssets.setData(reqRefund);

        APIResponse<List<TbUserAssets>> response = userAssetsService.refundSubmit(refundAssets);
        APIResponse<List<TbUserAssets>> response1 = userAssetsService.refundSuccess(refundAssets);
        logger.info("退款资产系统调用时间结束");
//        APIResponse<RespCoupon> apiResponse=couponService.getInfoByCode("166KVLRWT693");
//        ReqUserAccumulateModify reqUserAccumulateModify=new
//                APIResponse apiResponse=userAssetsService.modifyAccumulate();
        return new ResponseEntity<Object>(response, HttpStatus.OK);
//        ReqCoupon reqCoupon=new ReqCoupon();
//        reqCoupon.setUid(644);
//        reqCoupon.setStatus((byte)1);
//        reqCoupon.setEnabledAmount(new BigDecimal(270));
//        reqCoupon.setSportType((byte)1);
//        reqCoupon.setOrderType((byte)1);
//        apiRequest.setData(reqCoupon);
//        APIResponse<PageObj<List<RespCoupon>>> apiResponse=couponService.getOrderCouponList(apiRequest);
    }

    @RequestMapping(value="/test1",method= RequestMethod.POST)
    public ResponseEntity<Object> test1(HttpServletRequest request){
        int count=testService.test1();
        return new ResponseEntity<Object>(count, HttpStatus.OK);
    }

    @RequestMapping(value="/test2",method= RequestMethod.POST)
    public ResponseEntity<Object> test2(HttpServletRequest request){
        int count=testService.test2();
        return new ResponseEntity<Object>(count, HttpStatus.OK);
    }

    @RequestMapping(value="/test3",method= RequestMethod.POST)
    public ResponseEntity<Object> test3(HttpServletRequest request){
        TbUserAssets tbUserAssets = testService.test3();
        return new ResponseEntity<Object>(tbUserAssets, HttpStatus.OK);
    }

    @RequestMapping(value="/batchModifyEgg",method= RequestMethod.POST)
    public ResponseEntity<Object> batchModifyEgg(HttpServletRequest request){
        APIRequest apiRequest = new APIRequest();
        List<ReqUserAccumulateModify> reqUserAccumulateModifyList = new ArrayList<ReqUserAccumulateModify>();
        apiRequest.setData(reqUserAccumulateModifyList);
        ReqUserAccumulateModify reqUserAccumulateModify = new ReqUserAccumulateModify();
        reqUserAccumulateModify.setSystemCode("CLUB_SYSTEM_CODE");

        reqUserAccumulateModify.setReceiver(reqUserAccumulateModify.getReceiver()+"sgdsgga");
        reqUserAccumulateModify.setBusinessCode("CLUB_YEAR_ACTIVITY_CODE");
        reqUserAccumulateModify.setAssetsType(AssetsType.ASSETS_EGGS.id.byteValue());
        reqUserAccumulateModify.setUid(8);
        reqUserAccumulateModify.setReceiver(reqUserAccumulateModify.getReceiver()+"111");
        reqUserAccumulateModify.setPayReceive("测试");
        reqUserAccumulateModify.setAccumulate((long)10);
        reqUserAccumulateModify.setInfo("全民充能，主席砸蛋");
        reqUserAccumulateModifyList.add(reqUserAccumulateModify);
        ReqUserAccumulateModify reqUserAccumulateModify1 = new ReqUserAccumulateModify();
        reqUserAccumulateModify1.setSystemCode("CLUB_SYSTEM_CODE");
        reqUserAccumulateModify1.setBusinessCode("CLUB_YEAR_ACTIVITY_CODE");
        reqUserAccumulateModify1.setAssetsType(AssetsType.ASSETS_EGGS.id.byteValue());
        reqUserAccumulateModify1.setUid(10);
        reqUserAccumulateModify1.setReceiver("测试");
        reqUserAccumulateModify1.setPayReceive("测试");
        reqUserAccumulateModify1.setAccumulate((long)10);
        reqUserAccumulateModify1.setInfo("全民充能，主席砸蛋");
        reqUserAccumulateModifyList.add(reqUserAccumulateModify1);
        userAssetsService.batchModifyEgg(apiRequest);
        TbUserAssets tbUserAssets = testService.test3();
        return new ResponseEntity<Object>(tbUserAssets, HttpStatus.OK);
    }
}
